﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ComponentModel;

namespace HospitalSystem.Core
{
    public class GroupsLog
    {

        //สร้าง ps number พร้อมใส่รายละเอียด
        public void crepsnumber(Label HostID,Label StaffID)
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand command2 = new SqlCommand("INSERT INTO [ServiceProvision] (HostID,StaffID,PSDateTime) VALUES(@HostID,@StaffID,@PSDateTime)", scon);
            scon.Open();
            //command2.Parameters.AddWithValue("@PSNumber", psnumber.Text);
            command2.Parameters.AddWithValue("@HostID", HostID.Text);
            command2.Parameters.AddWithValue("@StaffID", StaffID.Text);
            command2.Parameters.AddWithValue("@PSDateTime", DateTime.Now);

            command2.ExecuteNonQuery();
            scon.Close();
        }

        //ส่งค่า psnumber ไปที่ label
        public void psnumbertolabel(Label psnumber)
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand CmdSql47 = new SqlCommand("SELECT PSNumber FROM [ServiceProvision] WHERE PSNumber = (SELECT MAX(PSNumber)  FROM [ServiceProvision])", scon);
            scon.Open();

            SqlDataReader reader = CmdSql47.ExecuteReader();
            // DataTable dt = new DataTable();
            //  dt.Load(reader);\
            // = reader;

            if (reader.Read())
            {
                psnumber.Text = reader["PSNumber"].ToString();



            }
            else
            {

            }
            scon.Close();
            reader.Close();


            
        }


        public void BindData(GridView GridView4)
        {
          
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT t2.CaseNo as CaseNo,t2.CID as CID,t3.FirstName as FirstName,t3.LastName as LastName FROM (SELECT cid,max(CaseNo) as caseno FROM [Evaluation] group by CID) t2 join Person t3 on t2.CID=t3.CID order by t3.FirstName", scon);
            SqlDataReader reader = cmd.ExecuteReader();

            GridView4.DataSource = reader;
            GridView4.DataBind();
            scon.Close();
        }


        public void inserttoServiceProvisionVsPerson(GridView GridView4, Label SelectStat,Label psnumber)
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            SelectStat.Text = string.Empty;
            bool selectorChecked2 = false;

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("CaseNo");
            dt2.Columns.Add("CID");
            // dt2.Columns.Add("CID");
            foreach (GridViewRow row in GridView4.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CaseSelector2");
                if (cb != null && cb.Checked)
                {
                    selectorChecked2 = true;
                    DataRow dr2;
                    dr2 = dt2.NewRow();
                    string CaseNo = GridView4.Rows[row.RowIndex].Cells[0].Text;
                    // string CID = GridView2.Rows[row.RowIndex].Cells[1].Text;
                    SelectStat.Text += string.Format("{0} has been added <br />", CaseNo);
                    string CID = GridView4.Rows[row.RowIndex].Cells[1].Text;
                    dt2.Rows.Add(CaseNo, CID);





                }
            }
            SelectStat.Visible = selectorChecked2;

            //GridView3.DataSource = dt2;
            //GridView3.DataBind();

            //เพิ่มข้อมูลที่เลือกลง db
            for (int i = 0; i < dt2.Rows.Count; i++)
            {


                SqlCommand command = new SqlCommand("INSERT INTO [ServiceProvisionVsPerson] (CaseNo,CID,PSNumber) VALUES('" + dt2.Rows[i]["CaseNo"].ToString() + "','" + dt2.Rows[i]["CID"].ToString() + "',@PSNumber)", scon);
                scon.Open();
                command.Parameters.AddWithValue("@PSNumber", psnumber.Text);
                command.ExecuteNonQuery();
                scon.Close();


            }



            //Response.Redirect("~/servicerecord2.aspx");
        }

    }
}